﻿using DAL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ExaminPurchaseBillDAL
    {
        private SqlCommand cmd;
        public ExaminPurchaseBillDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }
        /// <summary>
        /// 获取待审核订单列表
        /// </summary>
        public List<string> GetPendingPurchaseBill()
        {
            cmd.CommandText = "GetPendingPurchaseBill";
            cmd.Parameters.Clear();
            List<string> list = new List<string>();
            SqlDataReader reader = SQLHelper.ExcuteReader(cmd);//读数据库
            while (reader.Read())
            {
                string id = reader["PurchaseID"].ToString();
                list.Add(id);
            }
            reader.Close();//关闭读库
            return list;
        }
        /// <summary>
        /// 审核不通过
        /// </summary>
        public bool PassExamine(PurchaseBill bo)
        {
            SqlTransaction transaction = SQLHelper.ExcuteSqlTransaction(cmd);
            try
            {
                cmd.CommandText = "UpdateExaminerByPurchaseID";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;
                cmd.Parameters.Add("Examiner", SqlDbType.NVarChar, 6).Value = bo.Examiner;
                cmd.Parameters.Add("Memo", SqlDbType.NVarChar, 100).Value = bo.Memo;

                int num = cmd.ExecuteNonQuery();
                if (num > 0)
                {
                    transaction.Commit();
                    return true;
                }
                else
                    return false;
            }
            catch//(Exception ex)
            {
                transaction.Rollback();//回滚
                return false;
            }
            finally
            {
                SQLHelper.CloseConn();//关闭DB连接
            }
        }
        /// <summary>
        /// 审核不通过
        /// </summary>
        public bool NoPassExamine(PurchaseBill bo)
        {
            SqlTransaction transaction = SQLHelper.ExcuteSqlTransaction(cmd);
            try
            {
                cmd.CommandText = "NotPassExaminerByPurchaseID";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;
                cmd.Parameters.Add("Examiner", SqlDbType.NVarChar, 6).Value = bo.Examiner;
                cmd.Parameters.Add("Memo", SqlDbType.NVarChar, 100).Value = bo.Memo;

                int num = cmd.ExecuteNonQuery();
                if (num > 0)
                {
                    transaction.Commit();
                    return true;
                }
                else
                    return false;
            }
            catch//(Exception ex)
            {
                transaction.Rollback();//回滚
                return false;
            }
            finally
            {
                SQLHelper.CloseConn();//关闭DB连接
            }
        }

    }
}
